import fs from "fs";
import glob from "fast-glob";

async function start() {
  const HTMLFiles = await glob(["./**/*.html"], {
    ignore: ["./index.html", "**/node_modules/**"],
  });
  HTMLFiles.sort();
  const result = HTMLFiles.map((filepath) => {
    const arr = filepath.split("/");
    if (arr[0] === "react-examples") {
      return `<li><a href="${filepath}">react-examples/${arr[2]}</a></li>`;
    }
    return `<li><a href="${filepath}">${
      arr[1].startsWith("index.") ? arr[0] : filepath
    }</a></li>`;
  }).join("\n");
  fs.writeFile(
    "./index.html",
    `
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>ui</title>
  </head>
  <body>
  <!-- generated by ./index.mjs -->
  <h1>ui</h1>
  <ul>
  ${result}
  </ul>
  </body>
  </html>
  `,
    (err) => {}
  );
}

start();
